﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using tiDE.Model.Projects;
using System.IO;
using tiDE.Panels;
using tiDE.Support;
using ICSharpCode.AvalonEdit.Highlighting;
using System.Xml;
using tiDE.Support.CodeInsight;

namespace tiDE
{
    /// <summary>
    /// This application is the Windows-only WPF front-end
    /// to the tiDE tool suite
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Native Interop

        // These imports are used to take advantage of aero
        [StructLayout(LayoutKind.Sequential)]
        public class MARGINS
        {
            public int cxLeftWidth, cxRightWidth,
                cyTopHeight, cyBottomHeight;
        }

        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern bool DwmIsCompositionEnabled(); 

        #endregion

        #region Initialization

        public MainWindow()
        {
            InitializeComponent();
            LoadCodeInsight();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Set up application with pretty aero glass
            if (Environment.OSVersion.Version.Major >= 6 && DwmIsCompositionEnabled())
            {
                // Get the current window handle
                IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
                HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
                mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;

                this.Background = Brushes.Transparent;

                // Set the proper margins for the extended glass part
                MARGINS margins = new MARGINS();
                margins.cxLeftWidth = -1;
                margins.cxRightWidth = -1;
                margins.cyTopHeight = -1;
                margins.cyBottomHeight = -1;

                DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); // Returns error (not used)
            }

            SettingsProvider.Initialize();

            StartPage page = new StartPage();
            page.ShowAsDocument(dockManager);

            CodePanel panel = new CodePanel();
            panel.ShowAsDocument(dockManager);
        }

        /// <summary>
        /// Attempts to load layout from file, or creates
        /// a new layout.
        /// </summary>
        private void InitializeLayout()
        {
        }

        private void LoadCodeInsight()
        {
            CodePanel.LoadHighlighting(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Highlighting", "z80Highlighting.xshd"),
                "z80", new string[] { ".z80", ".asm" });
            // Why is this particular one broken?
            CodePanel.LoadHighlighting(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Highlighting", "AxeHighlighting.xshd"),
                "axe", new string[] { ".axe" });
            CodePanel.LoadHighlighting(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Highlighting", "CHighlighting.xshd"),
                "c", new string[] { ".c" });
            CodePanel.LoadHighlighting(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Highlighting", "TIBasicHighlighting.xshd"),
                "tib", new string[] { ".tib" });
            CodeInsightManager.Initialize();
        }

        #endregion

        private void exitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void AboutMenuItem_Click(object sender, RoutedEventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }
    }
}
